package com.health.mapp.impl;

import java.util.Date;

import com.health.db.model.RegisterOrder;
import com.health.db.model.RegisterOrderExample;
import com.health.db.model.UserAccountLedger;
import com.health.db.service.order.IRegisterOrderService;
import com.health.db.service.user.IUserAccountLedgerService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.UserApplyFinishOrderReq;
import com.health.mapp.bean.UserApplyFinishOrderResp;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;


/**
 *  此接口提供给用户操作
 *  用户对已就诊订单做确认接口
 *  调用后订单状态由就诊中修改为已完成状态
 *  用户的钱将从第三方账户中打入医生账户中
 *  此接口相当于在淘宝中做确认收货操作
 * 注：缺少融云消息推送
 * @author HiRoad
 * 
 * @pramati
      orderId	String	Y	订单号
 */
public class UserApplyFinishOrderImpl  implements ActionInterface{


	
	//service import
	private IRegisterOrderService registerOrderService ;
	private IUserLISTService userLISTService  ;
	private IUserAccountLedgerService userAccountLedgerService  ;
	
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		UserApplyFinishOrderReq req = (UserApplyFinishOrderReq) reqBean; 
		UserApplyFinishOrderResp _resp = new UserApplyFinishOrderResp();
		Long orderId = req.getOrderId();
		Long docId= req.getDocId();
		Long userId = req.getUserId();
		if (userId==0 || orderId==0 ) {
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}else{
			//RegisterOrder orderInfo = new RegisterOrder();
			
		 
			RegisterOrder  currRegisterOrder  =registerOrderService.selectSimpleSingleByOrderId(orderId);
			
			//判断订单状态
			if (currRegisterOrder==null) {
				_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
				_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
				return _resp;
			}
			
			//判断类型，状态必须是等待用户结诊才可以操作
			if (currRegisterOrder.getRunstatusid().intValue()!=Constant.CONSTANTS_ORDER_RUNSTATUS_ID_3.intValue()) {
				_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3004);
				_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3004_NOTE);
				return _resp;
			}
			
			int actionMethod =-1;
			//医生的账户
			UserAccountLedger accountLedger = new UserAccountLedger();
			//关闭时，修改为等待评价
			actionMethod =Constant.CONSTANTS_ORDER_ACTION_ID_4;
			currRegisterOrder.setRunStatus(Constant.CONSTANTS_ORDER_RUNSTATUS_DES_4);
			currRegisterOrder.setRunstatusid(Constant.CONSTANTS_ORDER_RUNSTATUS_ID_4);
			Date currDateTime=DateUtils.getDatetime();
			 
			currRegisterOrder.setUpdateDate(currDateTime);
			//医生账户表
			//关闭时，医生账户里获得相应的订单金额
			//分类账户更新数据
			//查询到当前医生的账户
			accountLedger = userAccountLedgerService.selectByUserId(docId);
			int accountType=Constant.CONSTANTS_ACCOUNT_EXIST_TYPE_0;
			//有账户则直接给账户增加订单金额
			if (null!=accountLedger&&accountLedger.getAcctpageid().intValue()!=0) {
				accountType=Constant.CONSTANTS_ACCOUNT_EXIST_TYPE_1;
				//医生获得的补偿累加
				accountLedger.setBalanceTotal(accountLedger.getBalanceTotal().add(currRegisterOrder.getRegisterPayoffTotal()));
			}
			else{
				//没有账户,要新创建一个账户
				accountLedger = new UserAccountLedger();
				accountLedger.setBalanceTotal(currRegisterOrder.getRegisterPayoffTotal());
				accountLedger.setAccountid(docId);
				accountLedger.setIsUsedFlag((short) 1);
			}
			
			//更新订单表，更新账户表
			RegisterOrderExample registerOrderExample= registerOrderService.updateOrder4Finish(currRegisterOrder, accountLedger, accountType);
		}
		
		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
 
		return _resp;
	}
	
	public IRegisterOrderService getRegisterOrderService() {
		return registerOrderService;
	}


	public void setRegisterOrderService(IRegisterOrderService registerOrderService) {
		this.registerOrderService = registerOrderService;
	}


	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}


	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}

	public IUserAccountLedgerService getUserAccountLedgerService() {
		return userAccountLedgerService;
	}

	public void setUserAccountLedgerService(
			IUserAccountLedgerService userAccountLedgerService) {
		this.userAccountLedgerService = userAccountLedgerService;
	}
	



}
